<!--original veriosn of addgroup-->
<?php
$page_title = 'eTAT: Add Group';
$style = 'addgroup';
$script = 'addgroup';
require_once 'top_header.php';
?>
<body>
<div id="wrapper">
<div id="page-container">
<?php
require_once 'header.php';


$course = $instructor->getCourse($_SESSION['add_project']->course);

if(isset($params->x)) {
	$body = '';
	for($c=1; $c<=$params->x; $c++) {
		$body .= "<div id='student$c'><span>$c. </span>";
		$body .= newText('full_name'.$c,'Full Name',array('class'=>'visible'));
		$body .= newText('gmail'.$c,'Gmail Address',array('class'=>'visible'));
		$body .= "</div>";
	}
	die($body);
}

if(isset($params->y)) {
	$body = '';
	for($c=1; $c<=$params->y; $c++) {
		$body .= "<div id='student$c'><span>$c. </span>";
		$body .= newText('gmail'.$c,'Gmail Address',array('class'=>'visible'));
		$body .= "</div>";
	}
	die($body);
}

if(!isset($_SESSION['add_project']->step)) {
	$_SESSION['add_project']->step = 0;
	redirect('addgroup.php');
}

if(isset($_POST['back_to_course'])) {
	$c = $_SESSION['add_project']->course;
	unset($_SESSION['add_project']);
	redirect("course.php?c=$c");
}

if(isset($_POST['cancel_add_project'])) {
	unset($_SESSION['add_project']);
	redirect('addgroup.php');
}

if(isset($_POST['group_info_submit'])) {
	$_SESSION['add_project']->group_name = $_POST['group_name'];
	$_SESSION['add_project']->group_description = $_POST['group_description'];
	$_SESSION['add_project']->student_count = $_POST['group_students'];
	$_SESSION['add_project']->step = 1;
	redirect('addgroup.php');
}

if(isset($_POST['project_search_accept'])) {
	$_SESSION['add_project']->google_code = $_POST['search_name'];
	$_SESSION['add_project']->step = 2;
	redirect('addgroup.php');
}

if(isset($_POST['group_search_accept'])) {
	$_SESSION['add_project']->google_groups = $_POST['search_name'];
	$_SESSION['add_project']->step = 3;
	redirect('addgroup.php');
}

if(isset($_POST['rechoose_option'])) {
	$_SESSION['add_project']->choice = '';
	$_SESSION['add_project']->step = 3;
	redirect('addgroup.php');
}

if(isset($_POST['option1_submit'])) {
	$_SESSION['add_project']->choice = 1;
	$_SESSION['add_project']->step = 4;
	redirect('addgroup.php');
}

if(isset($_POST['option2_submit'])) {
	$_SESSION['add_project']->choice = 2;
	$_SESSION['add_project']->step = 5;
	redirect('addgroup.php');
}

if(isset($_POST['csv_upload_submit'])) {
	$upload_path = $_FILES['csv_upload']['tmp_name'];
	if(!file_exists(ROOT . BRANCH . '/uploads'))
		mkdir(ROOT . BRANCH . '/uploads');
	$save_path = ROOT . BRANCH . '/uploads/'.$_FILES['csv_upload']['name'];
	copy($upload_path,$save_path);
	$_SESSION['add_project']->file_path = $save_path;
	$_SESSION['add_project']->step = 6;
	redirect('addgroup.php');
}

if(isset($_POST['students_submit']) || $_POST['csv_accept']) {
	if(isset($_POST['student_count'])) {
		$count = $_POST['student_count'];
	} else {
		$count = count($_POST);
		$_SESSION['student_count'] = $count;
	}
	for($i=1; $i<=$count; $i++) {
		$full_name = $_POST["full_name$i"];
		$gmail = $_POST["gmail$i"];
		$group_name = $_SESSION['add_project']->group_name;
		if($full_name != '' || $gmail != '')
			$students[] = (object) array('full_name'=>$full_name,'gmail'=>$gmail,'group'=>$group_name,'course'=>$course->id);
	}
	$_SESSION['add_project']->step = 10;
	$_SESSION['add_project']->students = $students;
	redirect('addgroup.php');
}

if(isset($_POST['option3_submit'])) {
	$_SESSION['add_project']->choice = 3;
	$_SESSION['add_project']->step = 7;
	redirect('addgroup.php');
}

if(isset($_POST['student_emails_submit'])) {
	if(isset($_POST['student_count'])) {
		$count = $_POST['student_count'];
	} else {
		$count = count($_POST)-2;
		$_SESSION['add_project']->student_count = $count;
	}
	for($i=1; $i<=$count; $i++) {
		$gmail = $_POST["gmail$i"];
		$group_name = $_SESSION['add_project']->group_name;
		if($gmail != '')
			$students[] = (object) array('gmail'=>$gmail,'group'=>$group_name,'course'=>$course->id);
	}
	$_SESSION['add_project']->students = $students;
	$_SESSION['add_project']->step = 10;
}

if(isset($_POST['option4_submit'])) {
	$_SESSION['add_project']->choice = 4;
	$_SESSION['add_project']->step = 10;
	redirect('addgroup.php');
}

if(isset($_POST['final_submit'])) {
	$data = $_SESSION['add_project'];
	$course->addGroup($data);
	redirect("course.php?c=$course->id");
}
?>

 <div id="categories">
    <?php include_once('./leftmenu.php');?>
  </div>
<h1><?php print $course->title; ?>: Add Group</h1>
<div id="main" >
<?php 
if($_SESSION['add_project']->step == 0) {
	$form = new Form('group_info_form','post');
	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->button('back_to_course','Cancel & Go back',array('class'=>'right'),false);
	$form->text("<h3>Enter basic group information:</h3>");
	$form->textInput('group_name','Group Name',array('required'=>true));
	$form->textArea('group_description','Description (optional)',3,20);
	$form->textInput('group_students','Number of Students (optional)');
	$form->button('group_info_submit','Continue',array('type'=>'submit'));	
	print $form->build();
}
?>

<?php
if($_SESSION['add_project']->step == 1) {
	$form = new Form('project_search_form','post');
	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->text("<h3>Search your project on Google Code:</h3>");
	$form->divStart('search_input');
	$form->textInput('search_name','Project Name (on Google Code)');
	$form->button('search_submit','Search Project',array('type'=>'button','onclick'=>'addProjectSearch("project",$("search-name").value);'));
	$form->divEnd();
	$form->divStart('search_output',array('class'=>'invisible'));
	$form->divEnd();
	$form->divStart('search_query',array('class'=>'invisible'));
	$form->text('<p>Is this the project that you wish to add? ');
	$form->button('project_search_accept','Yes, continue',array('type'=>'submit'),false);
	$form->button('search_deny','No, search again',array('type'=>'button','onclick'=>'resetAddProject();'),false);
	$form->text('</p>');
	$form->divEnd();
	print $form->build();
}
?>

<?php 
if($_SESSION['add_project']->step == 2) {
	$form = new Form('group_search_form','post');
	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->text("<h3>Add the corresponding group (for <b>{$_SESSION['add_project']->project_name}</b>) from Google Groups:</h3>");
	$form->divStart('search_input');
	$form->textInput('search_name','Group Name (on Google Groups)');
	$form->button('search_submit','Search Group',array('type'=>'button','onclick'=>'addProjectSearch("groups",$("search-name").value);'));
	$form->divEnd();
	$form->divStart('search_output',array('class'=>'invisible'));
	$form->divEnd();
	$form->divStart('search_query',array('class'=>'invisible'));
	$form->text('<p>Is this the right group associated with '.$_SESSION['add_project']->project_name.'? ');
	$form->button('group_search_accept','Yes, continue',array('type'=>'submit'),false);
	$form->button('search_deny','No, search again',array('type'=>'button','onclick'=>'resetAddProject();'),false);
	$form->text('</p>');
	$form->divEnd();
	print $form->build();
}
?>

<?php 
if($_SESSION['add_project']->step == 3) {
	$form = new Form('add_student_form','post');
	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->text("<h3>Add the Group Students (students from the <b>{$_SESSION['add_project']->project_name}</b> team):</h3>");
	$form->text('<br><br><div id="update-msg"><p>Note: This step cannot be performed automatically because Google 
			has strict privacy rules about sharing user data (like email and other account information) 
			over to third party applications.<br></div>
			<h2>You have four choices to perform this step:</h2></p><br>');
	$form->divStart('option1',array('class'=>'left'));
	$form->text('<h3>Option 1</h3><br>');
	$form->text('<p>You can type in the Full Name and Gmail Address of each student yourself (if you have the information)</p>');
	$form->button('option1_submit','Choose this',array('type'=>'submit'));
	$form->text('<br><h3>Option 2</h3><br>');
	$form->text('<p>You can upload a CSV file carrying the Full Name and Gmail Address of the students (if you have it in a CSV file)</p>');
	$form->button('option2_submit','Choose this',array('type'=>'submit'));
	$form->divEnd();
	$form->divStart('option2',array('class'=>'left'));
	$form->text('<h3>Option 3</h3><br>');
	$form->text('<p>You can send an email asking all your students to fill in their data themselves.You will need to enter their email addresses for this.</p>');
	$form->button('option3_submit','Choose this',array('type'=>'submit'));
	$form->text('<br><h3>Option 4</h3><br>');
	$form->text('<p>We have developed a learning model which will try to populate the Group Students 
			as the project updates keep coming into eTAT. This option might be a little inaccurate, though.</p>');
	$form->button('option4_submit','Choose this',array('type'=>'submit'));
	$form->divEnd();
	print $form->build();
}
?>

<?php 
if($_SESSION['add_project']->step == 4) {
	$form = new Form('add_student_form','post');
	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->button('rechoose_option','Choose other option',array('class'=>'right'),false);
	$form->text("<h3>You selected the First Option (Type in student information yourself):</h3>");
	$form->text('<br><br>');
	$form->textInput('student_count','Enter in number of students in this Group',array('size'=>'1'),false);
	$form->button('student_count_submit','Add',array('type'=>'button'),false);
	$form->text('<br><br>');
	$form->divStart('students');
	$form->divEnd();
	$form->text('<div class="clear"></div>');
	$form->button('students_submit','Finish',array('class'=>'invisible'));
	print $form->build();
}
?>

<?php 
if($_SESSION['add_project']->step == 5) {
	$form = new Form('csv_upload_form','post',array('enctype'=>'multipart/form-data'));
	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->button('rechoose_option','Choose other option',array('class'=>'right'),false);
	$form->text("<h3>You selected the Second Option (Upload CSV file):</h3>");
	$form->fileUpload('csv_upload','Choose File',array('required'=>true));
	$form->button('csv_upload_submit','Upload',array('type'=>'submit'),false);
	print $form->build();
}
?>

<?php 
if($_SESSION['add_project']->step == 6) {
	function build_list($names,$form) {
		$count = 1;
		$form->text("<table>");
		$form->text("<tr><th></th><th>Full Name</th><th>Gmail Address</th></tr>");
		$form->text("<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>");
		for($i=0;$i<count($names);$i++) {
			if($i % 2 == 0) {
				$j = $i+1;
				$form->text("<tr>");
				$form->text("<td width='20px'><input id='check-$count' type='checkbox' checked='checked' onclick='checkStudents($count);'></td>");
				$form->text("<td width='200px'>".newText("full_name$count",'',array('value'=>$names[$i],),false)."</td>");
				$form->text("<td>".newText("gmail$count",'',array('value'=>$names[$j]),false)."</td>");
				$form->text("</tr>");
				$count++;
			}
		}
		$form->text("<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>");
		$form->text("</table>");
	}
	$file = $_SESSION['add_project']->file_path;
	$list = file_get_contents($file);
	$names = preg_split('/[:|,\r\s]+/', trim($list), -1, PREG_SPLIT_NO_EMPTY);
	$count = 0;
	for($i = 0; $i < count($names); $i++) {
		if($i % 3 == 0) {
			$name[$count] = $names[$i].' '.$names[$i+1];
			$count++;
		} else if($i % 3 == 2) {
			$name[$count] = $names[$i];
			$count++;
		}
	}
	$_SESSION['add_project']->students = $name;
	
	$form = new Form('csv_upload_form','post',array('enctype'=>'multipart/form-data'));
	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->button('rechoose_option','Choose other option',array('class'=>'right'),false);
	$form->text("<h3>You selected the Second Option (Upload CSV file):</h3>");
	$form->fileUpload('csv_upload','Choose File');
	$form->button('csv_upload_submit','Upload',array('type'=>'submit'),false);
	$form->text("<br><br><p>We have parsed the uploaded CSV file using the best possible regular expressions. 
		<br>Have all the students' Full Names and Gmail Addresses been parsed properly?</p><br>");
	$form->divStart('csv_output');
	build_list($name,$form);
	$form->divEnd();
	$form->button('csv_accept','Yes, Accept');
	print $form->build();
}
?>

<?php 
if($_SESSION['add_project']->step == 7) {
	$form = new Form('csv_upload_form','post',array('enctype'=>'multipart/form-data'));
	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->button('rechoose_option','Choose other option',array('class'=>'right'),false);
	$form->text("<h3>You chose the Third Option (Send Email):</h3>");
	$form->textInput('student_count','Enter in number of students in this Group',array('size'=>'1'),false);
	$form->button('student_email_submit','Add',array('type'=>'button'),false);
	$form->text('<br><br>');
	$form->divStart('students');
	$form->divEnd();
	$form->button('student_emails_submit','Submit',array('type'=>'submit'),false);
	print $form->build();
}
?>

<?php 
if($_SESSION['add_project']->step == 8) {
	$form = new Form('csv_upload_form','post',array('enctype'=>'multipart/form-data'));
	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->button('rechoose_option','Choose other option',array('class'=>'right'),false);
	$form->text("<h3>You selected the Fouth Option (let eTAT do the work):</h3>");
	$form->button('send_email_submit','Upload',array('type'=>'submit'),false);
	print $form->build();
}
?>

<?php
if($_SESSION['add_project']->step == 10) {
	$x = $_SESSION['add_project'];
	$form = new Form('final_form','post');
//	$form->button('cancel_add_project','Start from stratch',array('class'=>'right'),false);
	$form->textInput('group_name','Group Name',array('value'=>$x->group_name,'required'=>true));
	$form->textArea('group_description','Group Description',3,25,array('value'=>$x->group_description));
	$form->textInput('google_code','Google Code URL',array('value'=>"http://code.google.com/p/$x->google_code/",'required'=>true),false);
	$form->button('check_google_code','Check',array('type'=>'button','onclick'=>'checkGoogleCode();'),false);
	$form->divStart('',array('class'=>'clear'));
	$form->divEnd();
	$form->textInput('google_groups','Google Groups URL',array('value'=>"http://groups.google.com/group/$x->google_groups",'required'=>true),false);
	$form->button('check_google_groups','Check',array('type'=>'button','onclick'=>'checkGoogleGroups();'),false);
	$count = 1;
	if($x->students) {
		$form->divStart('students');
		foreach($x->students as $m) {
			$form->divStart('student'.$count);
			$form->textInput('student_name'.$count,"$count. Full Name",array('value'=>$m->full_name,'required'=>true),false);
			$form->textInput('student_gmail'.$count,'Gmail Address',array('value'=>$m->gmail,'required'=>true),false);
			if($_SESSION['add_project']->choice == 3) {
				$form->text('Email will be sent.');
			}
			$form->button('remove_student','Remove',array('type'=>'button'),false);
			$form->divEnd();
			$count++;
		}
		?>
        <div id="right">
        <?php 
		$form->button('add_student','Add',array('type'=>'button'));
		$form->divEnd();
	}
//	$form->text('<br><span>Is this data correct? </span>');
	$form->button('final_submit','Add this group',array(),false);
	?></div>
     <?php
	print $form->build();
	$check_confirm = newBtn('check_confirm','Yes, this is correct',array('type'=>'button'),false);
	$check_cancel = newBtn('check_cancel','No, search again',array('type'=>'button'),false);
	$check_body = "
		<div id='check-output'></div>
		<div id='row-check-confirm' style='padding:10px;'>
		$check_confirm $check_cancel
		</div>";
	print popup('check-frame',$check_body,900,array('top'=>90,'left'=>40,'padding'=>0));
}
?>
</div>
<div class='clear'></div>
</div>
<?php require_once 'footer.php'; ?>
</div>
</body>
</html>